package com.hhq.demo.controller;
import java.util.ArrayList;
import java.util.Date;

import com.hhq.demo.bean.Express;
import com.hhq.demo.bean.ExpressList;
import com.hhq.demo.bean.ResultData;
import com.hhq.demo.mvc.ResponseBody;
import com.hhq.demo.service.ExpressService;
import com.hhq.demo.service.impl.ExpressServiceImpl;
import com.hhq.demo.utils.DateFormatUtil;
import com.hhq.demo.utils.JSONUtil;
import com.hhq.demo.utils.Message;
import com.hhq.demo.utils.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author HHQ
 * @date 2021年12月12日
 */
public class ExpressController {

    ExpressService expressService = new ExpressServiceImpl();

    @ResponseBody("/express/consoleData.do")
    public String consoleData(HttpServletRequest request, HttpServletResponse response){
        Message message = new Message();
        Map<String, Object> map = expressService.consoleData();
        if (map.isEmpty()) {
            message.setStatus(-1);
        } else {
            message.setStatus(0);
        }
        message.setData(map);
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/express/listLimit.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        Integer offset = Integer.parseInt(request.getParameter("offset"));
        Integer pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        List<Express> expressList = expressService.listLimit(offset, pageNumber);
        ArrayList<ExpressList> returnList = new ArrayList<>();
        for (Express e : expressList) {
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus()==0?"待取件":"已取件";
            String code = e.getCode()==null?"已取件":e.getCode();
            ExpressList e2 = new ExpressList(e.getId(),e.getNumber(),e.getCompany(),e.getReceiveName(),e.getReceivePhone(), e.getRecordPhone(), status,code,inTime,outTime);
            returnList.add(e2);
        }
        ResultData<ExpressList> resultData = new ResultData<>();
        resultData.setRows(returnList);
        resultData.setTotal((Integer) expressService.consoleData().get("data1_size"));
        return JSONUtil.toJSON(resultData);
    }

    @ResponseBody("/express/add.do")
    public String addExpress(HttpServletRequest request, HttpServletResponse response){
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String receivePhone = request.getParameter("receivePhone");
        String receiveName = request.getParameter("receiveName");
        Express express = new Express(number,company,receiveName,receivePhone, UserUtil.getUserPhone(request.getSession()));
        int flag = expressService.insert(express);
        Message msg = new Message();
        if(flag==1){
            msg.setStatus(0);
            msg.setResult("快递录入成功!");
        }else{
            msg.setStatus(-1);
            msg.setResult("快递录入失败!");
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/express/findByNumber.do")
    public String findByNumber(HttpServletRequest request, HttpServletResponse response){
        String number = request.getParameter("number");
        Express byNumber = expressService.findByNumber(number);
        Message message = null;
        if (byNumber!=null) {
            message = new Message(1,"查询成功",byNumber);
        } else {
            message = new Message(0,"该单号的快递不存在");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        String number = request.getParameter("number");
        Integer id = Integer.parseInt(request.getParameter("id"));
        String company = request.getParameter("company");
        String receiveName = request.getParameter("receiveName");
        String receivePhone = request.getParameter("receivePhone");
        String status = request.getParameter("status");
        Express express = new Express();
        express.setNumber(number);
        express.setCompany(company);
        express.setReceiveName(receiveName);
        express.setReceivePhone(receivePhone);
        express.setStatus(Integer.parseInt(status));
        int update = expressService.update(id, express);
        Message message = new Message();
        if (update==1) {
            message.setStatus(1);
            message.setResult("修改成功");
        } else {
            message.setStatus(0);
            message.setResult("修改失败");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/express/delete.do")
    public String deleteByNumber(HttpServletRequest request, HttpServletResponse response){
        String number = request.getParameter("number");
        int i = expressService.deleteByNumber(number);
        Message message;
        if (i>0) {
            message = new Message(1,"删除成功");
        } else {
            message = new Message(0,"删除失败");
        }
        return JSONUtil.toJSON(message);
    }
}
